package com.ellricky.baselib.ui.consutant;

import android.view.View;

import com.andr.common.ui.activity.BaseToolBarActivity;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.ToastUtil;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.ellricky.baselib.cache.LocalCacheUtil;
import com.ellricky.baselib.dialog.picker.EPickerEntityListener;
import com.ellricky.baselib.dialog.picker.EPickerUtil;
import com.ellricky.baselib.entity.IdNamePickerEntity;
import com.ellricky.baselib.http.HttpUtils;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.util.NewEntityCallback;
import com.ellricky.baselib.widget.EditItemView;
import com.flyco.roundview.RoundTextView;

import java.util.HashMap;
import java.util.Map;

public class CtSelectActivity extends BaseToolBarActivity<Void, BasePresenter<Void>> {

    private EditItemView etConsultant;
    private RoundTextView btnConfirm;
    private String id;

    private EPickerUtil ePickerUtil;
    private IdNamePickerEntity selCt;

    @Override
    public int getContentLayout() {
        return R.layout.base_activity_consultant_select;
    }

    @Override
    protected void initUIView() {
        super.initUIView();
        mTvTitle.setText(R.string.base_change_ct);

        id = getIntent().getStringExtra(BaseConstant.KEY_ID);

        etConsultant = findViewById(R.id.etConsultant);
        btnConfirm = findViewById(R.id.btnConfirm);

        ePickerUtil = new EPickerUtil(this);

        etConsultant.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
    }

    @Override
    protected void onClickView(View v) {
        super.onClickView(v);
        if (v == etConsultant) {
            ePickerUtil.checkCtPicker(selCt, new EPickerEntityListener<IdNamePickerEntity>() {
                @Override
                public void onSelectItem(int pickType, IdNamePickerEntity t) {
                    selCt = t;
                    etConsultant.setValue(t.getName());
                }
            });
            return;
        }

        if (v == btnConfirm) {
            postSubmit();
        }
    }


    private void postSubmit() {
        String ct = etConsultant.getValue();
        if (EmptyUtil.isEmpty(ct)) {
            ToastUtil.show(this, R.string.base_please_select_ct);
            return;
        }

        String url = EAppUtil.getApiHost();
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("action", "headAllotClueToSale");
        paramMap.put("clue_ids", id);
        paramMap.put("sale_id", selCt.getId());
        paramMap.put("eid", LocalCacheUtil.getUid());

        HttpUtils.getInstance().postFrom(url, paramMap, new NewEntityCallback<Object>(this) {
            @Override
            public void onSuccess(Object var1, String var2) {
                setResult(BaseConstant.RESULT_CODE_ADD);
                finish();
            }
        });
    }
}
